//
//  XJSureOrderHeaderView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/12.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  确认订单表头

#import "XJSureOrderHeaderView.h"

// model
#import "XJSureOrderAddressModel.h"
#import "XJAddressModel.h" // 收货地址控制器页面传递过来的model

@interface XJSureOrderHeaderView()
@property(nonatomic, strong) UIView *bgView;
@property(nonatomic, strong) UIImageView *iconIv;

//没有收货地址就显示这个 ,隐藏centerDiv
/**请选择收货地址*/
@property(nonatomic, strong) UILabel *pleaseSelectAddressLb;


// 选择地址后就显示这个centerDiv  隐藏 pleaseSelectAddressLb
/**中间div:姓名+手机号+地址*/
@property(nonatomic, strong) UIView *centerDiv;
@property(nonatomic, strong) UILabel *nameLb;// 姓名
@property(nonatomic, strong) UILabel *phoneLb;//手机号
@property(nonatomic, strong) UILabel *addressLb;//地址


@property(nonatomic, strong) UIImageView *arrowIv; // 箭头


// 点击事件:一个透明的大button
@property(nonatomic, strong) UIButton *bigClearBtn;


@end
@implementation XJSureOrderHeaderView
#pragma mark - 一 init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
        
        // 添加手势点
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSelectAddressGes)];
        [self addGestureRecognizer:tapGes];
    }
    return self;
}

- (void)setupView{
    [self addSubview:self.iconIv];
    
    [self addSubview:self.pleaseSelectAddressLb];

    
    [self addSubview:self.centerDiv];
    [self.centerDiv addSubview:self.nameLb];
    [self.centerDiv addSubview:self.phoneLb];
    [self.centerDiv addSubview:self.addressLb];

    
    
    [self addSubview:self.arrowIv];
}
#pragma mark - lazy
- (UIImageView *)iconIv {
    if (!_iconIv) {
        _iconIv = [UIImageView new];
        _iconIv.image = [UIImage imageNamed:@"address_list_on"];
    }
    return _iconIv;
}
- (UILabel *)pleaseSelectAddressLb {
    if (!_pleaseSelectAddressLb) {
        _pleaseSelectAddressLb = [UILabel new];
        _pleaseSelectAddressLb.text = @"请选择收货地址";
        _pleaseSelectAddressLb.textColor = [UIColor lightGrayColor];
        _pleaseSelectAddressLb.font = XJFONTSET_R(14.0);
    }
    return _pleaseSelectAddressLb;
}


//-----------------------------------------------
- (UIView *)centerDiv {
    if (!_centerDiv) {
        _centerDiv = [UIView new];
        // 目的:把 pleaseSelectAddressLb 盖住 遮羞
        _centerDiv.backgroundColor = [UIColor whiteColor];
        
    }
    return _centerDiv;
}
- (UILabel *)nameLb {
    if (!_nameLb) {
        _nameLb = [UILabel new];
        _nameLb.textAlignment = NSTextAlignmentCenter;
        _nameLb.text = @"张三";
    }
    return _nameLb;
}
- (UILabel *)phoneLb {
    if (!_phoneLb) {
        _phoneLb = [UILabel new];
        _phoneLb.text = @"1383838438";
        _phoneLb.font = [UIFont systemFontOfSize:16.0];
    }
    return _phoneLb;
}

- (UILabel *)addressLb {
    if (!_addressLb) {
        _addressLb = [UILabel new];
        _addressLb.text = @"浙江省杭州市滨江区华盛达广场江省杭州市滨江区华盛达广江省杭州市滨江区华盛达广江省杭州市滨江区华盛达广";
        _addressLb.numberOfLines = 0;
        _addressLb.textColor = [UIColor lightGrayColor];
        _addressLb.font = XJFONTSET_R(14.0);
    }
    return _addressLb;
}


//-----------------------------------------------


- (UIImageView *)arrowIv {
    if (!_arrowIv) {
        _arrowIv = [UIImageView new];
        _arrowIv.image = [UIImage imageNamed:@"direction_right_black"];
    }
    return _arrowIv;
}


#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat kLeftMargin = 15;
    
    [self.iconIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(kLeftMargin);
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(12, 15));
    }];
    
    // 没有地址就显示:请选择收货地址
    [self.pleaseSelectAddressLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconIv.mas_right).offset(10);
        make.right.equalTo(self.mas_right).offset(-40);
        make.centerY.equalTo(self.mas_centerY);
        make.height.mas_equalTo(30);
    }];
    
    
    // h:50 有地址就显示中间的centerDiv: 姓名+手机号+收货地址
    [self.centerDiv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconIv.mas_right).offset(10);
        make.right.equalTo(self.arrowIv.mas_left);
        make.centerY.equalTo(self.mas_centerY);
        make.height.mas_equalTo(70);
    }];
    //h:30
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.centerDiv);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(80);
    }];
    [self.phoneLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLb.mas_right);
//        make.bottom.equalTo(self.nameLb.mas_bottom);
        make.centerY.equalTo(self.nameLb.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(120, 20));
    }];
    // 高度
    [self.addressLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLb.mas_bottom);
        make.left.equalTo(self.centerDiv);
        make.right.equalTo(self.centerDiv.mas_right);
        make.bottom.equalTo(self.centerDiv.mas_bottom);
    }];


    [self.arrowIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-kLeftMargin);
        make.centerY.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(12, 12)); //
    }];
}

#pragma mark - 四 点击事件
- (void)tapSelectAddressGes {
    XJLog(@"点我干啥");
    
    if (self.selectAddressBlock) {
        self.selectAddressBlock();
    }
}

#pragma mark - 五 setter赋值 & 赋值
/**
 情况是这样的:
 情况一: 一进来就会做一次网络请求如果有数据直接赋值，如果没有数据不显示数据
 外界将字典传进来，然后转模型进行赋值。
 
 
 情况二: 点击头部 会跳转到编辑编辑收货地址页面。 选择之后 会传递进来一个字典。
 两种情况的字典不一样。
 所有要针对两种情况写两个方法进行赋值。来显示数据。
 🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️🤷‍♀️
 */
 // 情况一:
- (void)setAddressDic:(NSDictionary *)addressDic {
    if ([addressDic isKindOfClass:[NSNull class]]) {
        XJLog(@"字典为空,操作无效");
        
        // 如果没做数据就将 centerDiv 这个view隐藏
        self.centerDiv.hidden = YES;
        return;
    }
    
    self.centerDiv.hidden = NO;
    
    
    _addressDic = addressDic;
    // 字典转模型:
    XJSureOrderAddressModel *addressM = [XJSureOrderAddressModel mj_objectWithKeyValues:addressDic];
    
    // 赋值处理
    self.nameLb.text = addressM.name;
//    NSString *dizhi = [NSString stringWithFormat:@"%@%@%@%@",addressM.province,addressM.city,addressM.district,addressM.address];
    
    self.addressLb.text = addressM.address;
    self.phoneLb.text = addressM.phone;
}

// 情况二:
- (void)setAddressModel:(XJAddressModel *)addressModel {
    
    self.centerDiv.hidden = NO;
    self.nameLb.text = addressModel.name;
    self.phoneLb.text = addressModel.phone;
    self.addressLb.text = addressModel.address;
}



#pragma mark - 样式思路
/***
 情况一: 没有地址的时候。
 icon + "请选择收货地址Lb" + "箭头"
 
 情况二: 用地址
 icon +  "张三+15837694327  换行 浙江省杭州市滨江区华盛达广场110"  + "箭头"
 */
@end
